// STUNClient.h: interface for the CTurnClient class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUNCLIENT_H__BD697FDC_6959_4908_964F_9B6CC7A772E2__INCLUDED_)
#define AFX_STUNCLIENT_H__BD697FDC_6959_4908_964F_9B6CC7A772E2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "mysocket.h"
#include "mystring.h"
#include "osplatform.h"

#define STUN_ERROR					-1
#define STUN_UDPBLOCKED				0
#define STUN_OPEN					1
#define STUN_FULL_CONE				2
#define STUN_RESTRICTED_CONE		3
#define STUN_PORT_RESTRICTED_CONE	4
#define STUN_SYMMETRIC				5

class CTurnClient  
{
public:
	bool ProcessTimeout();
	bool StopRecord(const char *prid);
	bool StartRecord(const char *prid);
	void SetVideoFPS(int fps){m_nfps = fps;};
	void SetVideoFormat(int format){m_nVideoFormat = format;};
	void SetVideoWidth(int w){m_nVideoWidth = w;};
	void SetVideoHeight(int h){m_nVideoHeight = h;};
	void SetOwner(const char *powner){m_strOwner = powner;};
	void SetSubTitlePos(unsigned pos){m_nSubTitlePos = pos;};
	void SetTimeTitlePos(unsigned pos){m_nTimeTitlePos = pos;};
	void SetSubTitle(mystring &title){m_strSubTitle = title;};
	void SetStartTime(uint32 st){m_nStartTime = st;};
	void SetStopTime(uint32 et){m_nStopTime = et;};
	bool AllocateChannelRequest();
	bool ReleaseChannelRequest(const char *pchid);
	bool SendDataRequest();
	void FreeAllocateSocket();
	bool AllocateRequest(int livetime);
	bool AllocateRelayRequest(int livetime);
	bool IsOK(){return m_bAllocateOK;};
	uint32 GetAllocIP(){return m_nAllocIP;};
#ifdef IPV6_SUPPORT
	struct in6_addr GetAllocIP6(){ return m_AllocIPv6;};
#endif
	uint16 GetAllocPort(){return m_nAllocPort;};
	uint16 GetLocalPort(){return m_nLocalPort;};
	uint32 GetNATIP(){return m_nNATIP;};
	uint16 GetNATPort(){return m_nNATPort;};
	uint32 GetChannelIP(){return m_nChannelIP;};
#ifdef IPV6_SUPPORT
	struct in6_addr GetChannelIP6(){return m_nChannelIP6;};
#endif
	uint16 GetChannelPort(){return m_nChannelPort;};
	uint32 GetLifeTime(){return m_nLifeTime;};
	uint32 GetStartTime(){return m_nStartTime;};
	uint32 GetStopTime(){return m_nStopTime;};
	const mystring &GetChannelID(){return m_strChannelID;};

	unsigned GetSubTitlePos(){return m_nSubTitlePos;};
	unsigned GetTimeTitlePos(){return m_nTimeTitlePos;};
	const mystring &GetSubTitle(mystring &title){return m_strSubTitle;};

	int SharedSecretRequest();
	void SetUserName(const char *pusername){m_strUserName = pusername;};
	void SetPassword(const char *ppassword){m_strPassword = ppassword;};
	bool CreateSocketPair(SOCKET &dataSocket, SOCKET &controlSocket);
	int TestNATType();
	//void SetTimeout(int to){m_nTimeout = to;};
	void GetPayload(char *pdata,int len,char *pbuf,int &length);
	SOCKET GetSocket(){return m_s;};
	void SetServerAddress(const char *pip,unsigned short port){m_strServerIP = pip;m_nServerPort = port;};
	CTurnClient();
	CTurnClient(const char *ip,unsigned short port = 0);
	virtual ~CTurnClient();
private:
	int tlsread(char *pbuf,int len);
	BOOL		m_bAllocate;
	BOOL		m_bAllocateRelay;
	SOCKET		m_s;
	SOCKET		m_tls;
	mystring	m_strUserName;
	mystring	m_strPassword;
	mystring	m_strLocalIP;
	uint16		m_nLocalPort;	
	mystring	m_strServerIP;
	mystring	m_strTempUserName;
	mystring	m_strTempPassword;
	mystring	m_strChannelID;
	int			m_nServerPort;
	int			m_nPort;
	uint32		m_nStartTime;
	uint32		m_nStopTime;
	uint32		m_nLifeTime;
#ifdef IPV6_SUPPORT
	//struct in_addr6 m_AllocIPv6;
	//struct in_addr6 m_nNATIP6;/* do we need nat for ipv6 */
	//struct in_addr6 m_nChannelIP6;
	//struct in_addr6 m_nSourceIP6;
	struct in6_addr m_AllocIPv6;
	struct in6_addr m_nNATIP6;/* do we need nat for ipv6 */
	struct in6_addr m_nChannelIP6;
	struct in6_addr m_nSourceIP6;
#endif
	uint32		m_nAllocIP;
	uint16		m_nAllocPort;
	uint32		m_nNATIP;
	uint16		m_nNATPort;
	uint32		m_nSourceIP;
	uint16		m_nSourcePort;
	uint16		m_nChannelPort;
	uint32		m_nChannelIP;
	mystring	m_strRecordID;
	mystring	m_strOwner;
	int			m_nErrorCount;
	int			m_nfps;
	int			m_nVideoWidth;
	int			m_nVideoHeight;
	int			m_nVideoFormat;
	bool		m_bAllocateOK;
	unsigned	m_nSubTitlePos;
	unsigned	m_nTimeTitlePos;
	mystring	m_strSubTitle;
	time_t		m_nTimeout;
	uint32		m_nNextTime;
	int			m_nTryCount;
	int			m_nDeltaTime;
};

#endif // !defined(AFX_STUNCLIENT_H__BD697FDC_6959_4908_964F_9B6CC7A772E2__INCLUDED_)
